import re
import logging
from odoo import fields, api, models, tools, _
from odoo.exceptions import UserError, ValidationError

_logger = logging.getLogger(__name__)

class Respondent(models.Model):
    _name = 'respondent.address'
    _description = 'Respondent Address'

    respondent_unit_id = fields.Many2one('respondent.unit', string='单位地址', required=True)
    address = fields.Char('单位地址', required=True)
    name = fields.Char(compute='_compute_name')
    default = fields.Boolean(string='默认地址', default=False)


    @api.constrains('default')
    def _constrains_default(self):
        address_obj = self.sudo().search([('respondent_unit_id', '=', self.respondent_unit_id.id),('default', '=', True)])
        if len(address_obj) > 1:
            raise ValidationError("一个单位下 只能有一个默认地址")

    def _compute_name(self):
        for rec in self:
            rec.name = str(rec.respondent_unit_id.name) + '(' + str(rec.address) + ')'
